<?php

namespace App\Http\Controllers\Admin;

use App\Exports\EnterLogExport;
use App\Http\Filters\EnterLogFilter;
use App\Models\EnterLog;
use App\Http\Requests\EnterLogRequest;
use App\Http\Resources\EnterLogResource;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;

class EnterLogController extends Controller
{
    public function index(EnterLogFilter $filter)
    {
        $enterLogs = EnterLog::query()->with('housings','user','housingsorder')
            ->filter($filter)
            ->paginate();

        return $this->ok(EnterLogResource::collection($enterLogs));
    }

    public function create()
    {
        return $this->ok();
    }

    public function store(EnterLogRequest $request)
    {
        $inputs = $request->validated();
        $enterLog = EnterLog::create($inputs);

        return $this->created(EnterLogResource::make($enterLog));
    }

    public function edit(Request $request, EnterLog $enterLog)
    {
        return $this->ok(EnterLogResource::make($enterLog));
    }

    public function update(EnterLogRequest $request, EnterLog $enterLog)
    {
        $inputs = $request->validated();
        $enterLog->update($inputs);

        return $this->created(EnterLogResource::make($enterLog));
    }

    public function destroy(EnterLog $enterLog)
    {
        $enterLog->delete();
        return $this->noContent();
    }

    public function export(Request $request)
    {
        $filters = $request->only([
            'ids',
            // ... 其他可能的过滤条件
        ]);
        return Excel::download(new EnterLogExport($filters), '入住记录.xlsx');
    }
}
